<template>
  <div style="padding: 10px">
    <div style="margin-bottom: 10px">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        v-hasPermi="['business:unit:add']"
        >新增</el-button
      >
    </div>
    <el-table v-loading="loading" :data="dataList" border>
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column
        label="单位名称"
        align="center"
        prop="name"
      ></el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:unit:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['business:unit:remove']"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
        align="center"
        :hide-on-single-page="false"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-sizes="[10, 15, 20, 30]"
        :page-size="10"
        :current-page="queryParams.pageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="单位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入单位名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addUnitApi,
  getUnitListApi,
  updateUnitApi,
  removeUnitInfo,
} from "@/api/product/product.js";
export default {
  name: "unitSetting",
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
      },
      dataList: [],
      total: 0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "单位名称不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getUnitList();
  },
  methods: {
    // 新增单位
    async addUnit() {
      await addUnitApi(this.form);
      this.open = false;
      this.getUnitList();
      this.$message.success("新增成功");
    },
    // 修改单位
    async editUnit() {
      await updateUnitApi(this.form);
      this.open = false;
      this.getUnitList();
      this.$message.success("修改成功");
    },

    // 单位列表
    async getUnitList() {
      const { data: res } = await getUnitListApi(this.queryParams);
      this.dataList = res.list;
      this.total = res.total;
      this.loading = false;
    },
    //分页修改每页显示条数
    handleSizeChange(val) {
      this.queryParams.pageSize = val;
      this.getUnitList();
    },
    //分页跳转页面
    handleCurrentChange(val) {
      this.queryParams.pageNum = val;
      this.getUnitList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form.id = row.id;
      this.form.name = row.name;
      this.title = "修改单位";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form.id);
          if (this.title == "修改单位") {
            this.editUnit();
          } else {
            this.addUnit();
          }
        }
      });
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      console.log(row.id);
      await removeUnitInfo(row.id);
      this.getUnitList();
      this.$message.success("删除成功");
    },
  },
};
</script>

<style scoped></style>
